package com.lanou3g.dllo.baidumusic.musiclibrary.recommend;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseFragment;
import com.lanou3g.dllo.baidumusic.main.MainActivity;
import com.lanou3g.dllo.baidumusic.musiclibrary.recommend.songsrecommenddetail.SongsRecommendAdapter;
import com.lanou3g.dllo.baidumusic.musiclibrary.recommend.songsrecommenddetail.SongsRecommendBean;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.MyVolley;
import com.lanou3g.dllo.baidumusic.urlvalues.UrlValues;
import com.youth.banner.Banner;


/**
 * Created by dllo on 16/6/17.
 */
public class RecommendFragment extends BaseFragment {

    private RecyclerView recommendRecycler,recommendSongsRecommendRecyclerview;
    private Banner recommendBanner;
    private SongsRecommendAdapter songsRecommendAdapter;
    private SongsRecommendBean songsRecommendBean;
    private TextView recommendSongsRecommentMoreBtn;
    private ImageView recommendAllSingerIv;

    @Override
    public int setLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void initView(View view) {

        recommendSongsRecommendRecyclerview = (RecyclerView) view.findViewById(R.id.recommend_songsrecommend_recyclerview);
        recommendRecycler = (RecyclerView) view.findViewById(R.id.recommend_recycler);
        RecyclerViewHeader header = RecyclerViewHeader.fromXml(context,R.layout.item_banner);
        recommendBanner = (Banner) header.findViewById(R.id.recommend_banner);
        // 设置下面跟着走的点的方法
        recommendBanner.setBannerStyle(Banner.CIRCLE_INDICATOR);
        recommendBanner.setIndicatorGravity(Banner.CENTER);

        recommendRecycler.setLayoutManager(new LinearLayoutManager(context));
        header.attachTo(recommendRecycler);

        songsRecommendAdapter = new SongsRecommendAdapter();
        songsRecommendAdapter.setMyRvOnClickListener(new MyRvOnClickListener() {
            @Override
            public void onClick(int position) {
                // 复用PlaylistDetail的Fragment
                MainActivity mainActivity = (MainActivity) getActivity();
                String listid = songsRecommendBean.getContent().getList().get(position).getListid();
                mainActivity.toPlaylistDetail(listid);
            }
        });

        recommendSongsRecommentMoreBtn = (TextView) view.findViewById(R.id.recommend_songsrecomment_more_btn);
        recommendSongsRecommentMoreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context.getPackageName() + ".TOPLAYLISTFRAGMENT");
                context.sendBroadcast(intent);
            }
        });

        // 推荐 全部歌手 跳转
        recommendAllSingerIv = (ImageView) view.findViewById(R.id.recommend_allsinger_iv);
        recommendAllSingerIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity mainActivity = (MainActivity) getActivity();
                mainActivity .toAllSinger();
            }
        });
    }

    @Override
    public void initData() {
        // 轮播图解析
        MyVolley.getMyVolley().initGson(UrlValues.AUTO_DISPLAY_PHOTOS, new MyVolley.GetGsonListener<AutoDisplayPhotos>() {
            @Override
            public void getGsonListener(AutoDisplayPhotos data) {
               String[] imageURL = new String[data.getPic().size()];
                for (int i = 0; i < data.getPic().size() ; i++) {
                    imageURL[i] = data.getPic().get(i).getRandpic();
                    recommendBanner.setDelayTime(3500);
                }
                recommendBanner.setImages(imageURL);
            }
        }, AutoDisplayPhotos.class);

        // 推荐界面:歌单推荐解析
        MyVolley.getMyVolley().initGson(UrlValues.SONGS_RECOMMEND_URL, new MyVolley.GetGsonListener<SongsRecommendBean>() {
            @Override
            public void getGsonListener(SongsRecommendBean data) {
                songsRecommendBean = data;
                songsRecommendAdapter.setSongsRecommendBean(data);
            }
        },SongsRecommendBean.class);

        recommendSongsRecommendRecyclerview.setAdapter(songsRecommendAdapter);
        recommendSongsRecommendRecyclerview.setLayoutManager(new GridLayoutManager(context,3));

    }

}

